﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using L.M.System.Infrastruecure.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace L.M.System.Application.Services
{
    public class System_number_tableServices : BaseServices<system_number_table, string>, ISystem_number_tableServices
    {
        /// <summary>
        /// 系统编号
        /// </summary>
        private readonly ISystem_number_tableRepository system_Number_TableRepository;

        public System_number_tableServices(ISystem_number_tableRepository system_Number_TableRepository) : base(system_Number_TableRepository)
        {
            this.system_Number_TableRepository = system_Number_TableRepository;
        }

        /// <summary>
        /// 系统编号查询 + 分页 
        /// </summary>
        /// <param name="tName">模糊查询</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns>系统编号查询 + 分页 </returns>
        public async Task<ApiReturn<PageETO<system_number_table>>> GetSystem_number_table(int pageSize, int pageIndex, string? tName)
        {
            ApiReturn<PageETO<system_number_table>> apiReturn = new ApiReturn<PageETO<system_number_table>>();
            Expression<Func<system_number_table, bool>> i = c => true;
            if (!string.IsNullOrEmpty(tName))
            {
                i = c => c.system_name.Contains(tName);
            }
            var list = await system_Number_TableRepository.GetShowQueryableAsync(pageSize, pageIndex, i);
            apiReturn.Code = 200;
            apiReturn.Message = "成功";
            apiReturn.ObjInfor = list;
            return apiReturn;
        }

        /// <summary>
        /// 修改系统更新编号数据(MQ)
        /// </summary>
        /// <param name="newNumber"></param>
        /// <returns></returns>
        public ApiReturn<string> GetNewNumber(string? newNumber)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            lock (this)
            {
                var array = newNumber.Split("-");
                var sysnumber = system_Number_TableRepository.GetShowObj(c => c.system_name == array[0]);
                var systemnumber = (Convert.ToInt32(sysnumber.system_serial_number) + 1).ToString();
                sysnumber.system_serial_number = systemnumber.PadLeft(4, '0');
                sysnumber.system_number_serial = sysnumber.system_prefix + sysnumber.system_month + sysnumber.system_date + sysnumber.system_serial_number;
                var res = system_Number_TableRepository.UpdInfor(sysnumber);
                if (res > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "修改成功";
                }
            }
            return apiReturn;
        }

        /// <summary>
        /// 每日定时修改编号
        /// </summary>
        /// <returns></returns>
        public async Task<ApiReturn<system_number_table>> UpdSystemInfor()
        {
            try
            {
                //开启事务
                system_Number_TableRepository.BeginTranInfor();
                ApiReturn<system_number_table> apiReturn = new ApiReturn<system_number_table>();
                // 获取当前月份的英文名称
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                string[] monthArray = new string[]
                {
                "1_January",
                "2_February",
                "3_March",
                "4_April",
                "5_May",
                "6_June",
                "7_July",
                "8_August",
                "9_September",
               "10_October",
                "11_Nov",
                "12_Ember"
                };
                foreach (var item in monthArray)
                {
                    var arr = item.Split("_");
                    keyValuePairs[arr[0]] = arr[1];
                }

                // 获取当前的月份
                DateTime currentDateTime = DateTime.Now;
                int dayMonmer = Convert.ToInt32(currentDateTime.ToString("MM"));
                //获取到当前的月份
                string monthName = keyValuePairs[dayMonmer.ToString()];
                //获取到时间值 如 20240411
                var marNumber = currentDateTime.ToString("yyyyMMdd");

                var list = await system_Number_TableRepository.GetShowInforAsync();
                List<system_number_table> system_s = new List<system_number_table>();
                foreach (var item in list)
                {
                    item.system_serial_number = "0001";
                    item.system_month = monthName;
                    item.system_date = marNumber;
                    item.system_number_serial = item.system_prefix + monthName + marNumber + "0001";
                    system_s.Add(item);
                }
                await system_Number_TableRepository.UpdInfor_Batch_Async(system_s);
                apiReturn.Code = 200;
                apiReturn.Message = "成功";
                apiReturn.ListInfor = system_s;
                system_Number_TableRepository.CommitTranInfor();
                return apiReturn;
            }
            catch (Exception)
            {
                system_Number_TableRepository.RollbackTranInfor();
                throw;
            }
        }
    }
}
